﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Xml;
using Com.Parking.BO;


public partial class Frame : System.Web.UI.MasterPage
{
    int temp = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        Inits();
    }


    #region Private Methods


    private void Inits()
    {

        //Get employee from session
        Employee em = Utils.GetEmployeeSession();
        string strPer = em.PermissionID;

        //Only permit 3 permissions: Supper Admin,Admin,Manager
        if (Constants.SECURITY.Equals(strPer) || Constants.ACCOUNTANT.Equals(strPer))
            Response.Redirect(Constants.LOGIN);

      
        //Handle menu
        string strPage = Request.QueryString["page"];

        //if null, set default page is user
        if (String.IsNullOrEmpty(strPage))
            strPage = "1";
        temp = Int16.Parse(strPage);

        //bind to repeater
        Repeater1.DataSource = LoadMenuXML();
        Repeater1.DataBind();

        //Set user information
        Employee emp = Utils.GetEmployeeSession();
        lbtnUser.Text = emp.FirstName + Constants.SPACE + emp.LastName;
        lblLoginDate.Text = emp.LastLogin.ToString(Constants.TIME_PATTERN + Constants.SPACE + Constants.DATE_PATTERN);
       
    }

    /// <summary>
    /// Load xml file to arraylist
    /// </summary>
    /// <returns></returns>
    private ArrayList LoadMenuXML()
    {
        string xmlFile = Server.MapPath(@"includes/menu.xml");
        ArrayList menuArr = new ArrayList();
        try
        {
            XmlReader reader = XmlReader.Create(xmlFile);
            int i = 1;
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    //find menu tag
                    if (reader.Name == "menu")
                    {
                        ParkingMenu menu = new ParkingMenu();

                        //get atribute from xml file in 'menu' tag
                        reader.MoveToAttribute("id");
                        menu.Id = reader.Value;                        
                        reader.MoveToAttribute("page");
                        menu.Page = reader.Value;                        
                        reader.MoveToAttribute("url");
                        menu.Url = reader.Value;
                        reader.MoveToAttribute("last");
                        menu.Last = reader.Value;

                        //handle selected item
                        if (i == temp)
                        {
                            //set selected button
                            menu.Last = Constants.SELECTED;

                            //set selected button for last item
                            if (!String.IsNullOrEmpty(reader.Value))
                                menu.Last = Constants.LAST + Constants.SPACE + Constants.SELECTED;
                        }
                        menuArr.Add(menu);
                        i++;
                    }
                    reader.MoveToElement();
                }

            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
        return menuArr;
    } 

    #endregion

    #region Events
    protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int noMenu = LoadMenuXML().Count;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Literal lblLast = (Literal)e.Item.FindControl("litLast");
            if (e.Item.ItemIndex == noMenu - 1)
            {
                lblLast.Visible = true;
            }
            else
                lblLast.Visible = false;
        }
    }


    protected void btnShiftGrant_Click(object sender, EventArgs e)
    {
        this.Context.Items["Menu"] = "SHIFT_GRANT";
        Server.Transfer(Constants.EMPLOYEE_SEARCH);
    }
    protected void btnTimeTracking_Click(object sender, EventArgs e)
    {
        this.Context.Items["Menu"] = "TIME_TRACKING";
        Server.Transfer(Constants.EMPLOYEE_SEARCH);
    }
    protected void lbtnLogout_Click(object sender, EventArgs e)
    {
        Session.Remove("Employee");
        Response.Redirect("login.aspx");
    } 
    
    protected void lbtnUser_Click(object sender, EventArgs e)
    {
        this.Context.Items["id"] = Utils.GetEmployeeSession().Id;
        Server.Transfer(Constants.EMPLOYEE_ACTION);
    }
    #endregion
}
